// 导入路由必要的工具函数（你的默认文件已经有了）
import { createRouter, createWebHistory } from 'vue-router'

// 1. 导入你创建的组件（注意路径要写对）
//import 路径别名 from '路径'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'  // 修复拼写错误：原来是 Resiger，现在是 Register
import ORmenu from '@/views/menu/OR_menu.vue'
import MAmenu from '@/views/menu/MA_menu.vue'
import USERmenu from '@/views/menu/USER_menu.vue'
import ORShowInfo from '@/views/organizer/OR_show_info.vue'
import MACreateAccount from '@/views/manager/MA_create_account.vue'
import MA_AccountManager_Menu from '@/views/manager/MA_AccountManager_menu.vue'
import MAShowInfo from '@/views/manager/MA_show_info.vue'
import MAChangeInfo from '@/views/manager/MA_change_info.vue'
import OR_qua_app from '@/views/organizer/OR_qua_app.vue'
import OR_new_qua_app from '@/views/organizer/OR_new_qua_app.vue'
import OR_show_place from '@/views/organizer/OR_show_place.vue'
import OR_place_app from '@/views/organizer/OR_place_app.vue'
import OR_new_place_app from '@/views/organizer/OR_new_place_app.vue'
import OR_show_game from '@/views/organizer/OR_show_game.vue'
import OR_Change_gameInfo from '@/views/organizer/OR_Change_gameInfo.vue'
import OR_gameEvent_info from '@/views/organizer/OR_gameEvent_info.vue'
import OR_new_gameEvent from '@/views/organizer/OR_new_gameEvent.vue'
import OR_eventRegister from '@/views/organizer/OR_eventRegister.vue'
import OR_show_schedule from '@/views/organizer/OR_show_schedule.vue'
import OR_new_schedule from '@/views/organizer/OR_new_schedule.vue'
import OR_show_scoreManager from '@/views/organizer/OR_show_scoreManager.vue'
import OR_score_order from '@/views/organizer/OR_score_order.vue'
import OR_write_score from '@/views/organizer/OR_write_score.vue'
import OR_show_VA from '@/views/organizer/OR_show_VA.vue'
import OR_new_VA from '@/views/organizer/OR_new_VA.vue'
import OR_show_VE from '@/views/organizer/OR_show_VE.vue'
import OR_show_notice from '@/views/organizer/OR_show_notice.vue'
import OR_new_notice from '@/views/organizer/OR_new_notice.vue'
import OR_show_mynotice from '@/views/organizer/OR_show_mynotice.vue'
// 在 router/index.ts 的 routes 数组中添加 user-center 相关路由
import UserCenter from '@/views/user-center/index.vue'
import UserProfile from '@/views/user-center/profile/index.vue'
import ProfileForm from '@/views/user-center/profile/ProfileForm.vue'
import UserEvents from '@/views/user-center/events/index.vue'
import UserVolunteer from '@/views/user-center/volunteer/index.vue'
import VolunteerApplyForm from '@/views/user-center/volunteer/ApplyForm.vue'
// 2. 创建路由实例（你的默认文件已有，重点改 routes 数组）
const router = createRouter({
  // 用 History 模式，BASE_URL 是项目的基础路径（从环境变量来，不用改）
  history: createWebHistory(import.meta.env.BASE_URL),
  // 3. 填充路由规则：path（路径）对应 component（组件）
  routes: [
    {// 添加默认路由，当访问根路径时重定向到登录页面
      path: '/',
      redirect: '/Login'
    },
    {//登录页面
      path: '/Login', // 访问根路径 http://localhost:5173/Login 时
      component: Login // 显示 Login 组件
    },
    {//注册页面
      path: '/Register',  // 修复路径拼写错误：原来是 /Resiger，现在是 /Register
      component: Register // 修复组件名：原来是 Resiger，现在是 Register
    },
    {//举办方操作菜单页面
      path: '/ORmenu',
      component: ORmenu
    },
    {//管理员操作菜单页面
      path: '/MAmenu',
      component: MAmenu
    },
    {//用户操作页面
      path: '/USERmenu',
      component: USERmenu
    },
    {//举办方信息展示页面
      path: '/ORmenu/ORShowInfo',
      component: ORShowInfo
    },
    {//主管理员创建账户页面
      path: '/MAmenu/MACreateAccount',
      component: MACreateAccount
    },
    {//主管理员账户管理页面
      path: '/MAmenu/MA_AccountManager_Menu',
      component: MA_AccountManager_Menu
    },
    {//主管理员信息展示页面
      path: '/MAmenu/MAShowInfo',
      component: MAShowInfo
    },
    {//主管理员信息修改页面
      path: '/MAmenu/MAChangeInfo',
      component: MAChangeInfo
    },
    {//举办方申办资格申请页面
      path: '/ORmenu/OR_qua_app',
      component: OR_qua_app
    },
    {//举办方新建资格申请页面
      path: '/ORmenu/OR_new_qua_app',
      component: OR_new_qua_app
    },
    {//举办方场地展示页面
      path: '/ORmenu/OR_show_place',
      component: OR_show_place
    },
    {//举办方个人场地申请页面
      path: '/ORmenu/OR_place_app',
      component: OR_place_app
    },
    {//举办方发起场地申请页面
      path: '/ORmenu/OR_new_place_app',
      component: OR_new_place_app
    },
    {//举办方比赛展示页面
      path: '/ORmenu/OR_show_game',
      component: OR_show_game
    },
    {//举办方修改比赛信息页面
      path: '/ORmenu/OR_Change_gameInfo',
      component: OR_Change_gameInfo
    },
    {//举办方比赛信息页面
      path: '/ORmenu/OR_gameEvent_info',
      component: OR_gameEvent_info
    },
    {//举办方新建比赛项目页面
      path: '/ORmenu/OR_new_gameEvent',
      component: OR_new_gameEvent
    },
    {//举办方比赛项目报名页面
      path: '/ORmenu/OR_eventRegister',
      component: OR_eventRegister
    },
    {//举办方比赛项目日程展示页面
      path: '/ORmenu/OR_show_schedule',
      component: OR_show_schedule
    },
    {//举办方新建比赛项目日程页面
      path: '/ORmenu/OR_new_schedule',
      component: OR_new_schedule
    },
    {//举办方比赛项目成绩管理页面
      path: '/ORmenu/OR_show_scoreManager',
      component: OR_show_scoreManager
    },
    {//举办方比赛项目成绩排序页面
      path: '/ORmenu/OR_score_order',
      component: OR_score_order
    },
    {//举办方比赛项目成绩录入页面
      path: '/ORmenu/OR_write_score',
      component: OR_write_score
    },
    {//举办方志愿者管理页面
      path: '/ORmenu/OR_show_VA',
      component: OR_show_VA
    },
    {//举办方新建志愿者页面
      path: '/ORmenu/OR_new_VA',
      component: OR_new_VA
    },
    {//举办方志愿者管理页面
      path: '/ORmenu/OR_show_VE',
      component: OR_show_VE
    },
    {//举办方通知管理页面
      path: '/ORmenu/OR_show_notice',
      component: OR_show_notice
    },
    {//举办方新建通知页面
      path: '/ORmenu/OR_new_notice',
      component: OR_new_notice
    },
    {//举办方我的通知页面
      path: '/ORmenu/OR_show_mynotice',
      component: OR_show_mynotice
    },
    // 添加 user-center 路由
    {
      path: '/user-center',
      name: 'UserCenter',
      component: UserCenter,
      redirect: '/user-center/profile',
      children: [
        // 个人信息相关路由
        {
          path: 'profile',
          name: 'UserProfile',
          component: UserProfile
        },
        {
          path: 'profile/edit',
          name: 'ProfileEdit',
          component: ProfileForm
        },
        // 活动管理相关路由
        {
          path: 'events',
          name: 'UserEvents',
          component: UserEvents
        },
        // 志愿者相关路由
        {
          path: 'volunteer',
          name: 'UserVolunteer',
          component: UserVolunteer
        },
        {
          path: 'volunteer/apply',
          name: 'VolunteerApply',
          component: VolunteerApplyForm
        }
      ]
    }
  ]
})


//添加一个路由守卫
router.beforeEach((to, from, next) => {
  //验证token，只有存在token的时候，才能访问内容页
  const token = localStorage.getItem('token')
    console.log('当前token值:', token) // 查看token实际值

  // 如果访问登录或注册页面，直接放行
  if (to.path === '/Login' || to.path === '/Register') {
    console.log('前往放行页面')
    next()
    return
  }

  // 如果有token，放行
  if (token) {
    console.log('token存在')
    next()
    return
  }

  // 如果没有token且访问受保护页面，重定向到登录页
  console.log('token不存在,请先登录')
  next('/Login')


  //在main.ts文件中清空了token，每次重新启动前端页面都把token清空
})

export default router
